import { Injectable } from '@angular/core';
import { HttpClient } from "@angular/common/http";

import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { MainFormatService } from '../../core/services/main-format.service';
@Injectable({
  providedIn: 'root'
})
export class PlotService {

  constructor(
    private http: HttpClient,
    private mainFormatService: MainFormatService,
    private modal: NzModalService
  ) { }
  // 合并服务
  public getList(serviceID, params) {
    return this.http.get<any>(`mock-data/${serviceID}.json`, { params })
  }
  /**
   *
   * @param title 模态窗标题
   * @param component 组件
   * @param params   传入参数
   */
  openModel(title, component, params) {
    const modal = this.modal.create({
      nzTitle: title,
      nzContent: component,
      nzWidth: '60%',
      nzGetContainer: () => document.body, // 返回当前文档中的<body>元素或者<frameset>元素
      nzComponentParams: {
        Url: params
      },
      nzOnOk: () => { },
      nzFooter: null
    });
    modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));
    // Return a result when closed
    modal.afterClose.subscribe(result => console.log('[afterClose] The result is:', result));
  }

  /**
   * 根据逗号分隔字符串
   *  @param element string
   *  返回 Array
   */
  getObjPath(element) {
    const pdfArr = [];
    let pathArrStr = null;
    if (element.indexOf(',') > -1) {
      pathArrStr = element.split(',');
    } else {
      pathArrStr = [element];
    }
    pathArrStr.forEach(element1 => {
      const names = (element1.split('/')[element1.split('/').length - 1]);
      pdfArr.push({ name: names, path: element1 });
    });
    return pdfArr;
  }
  dataFormat(data) {
    if (!Array.isArray(data)) {
      return;
    }
    data.forEach(element => {
      if (element.bgcflj && element.bgcflj.length > 1) {
        element.bgcflj = this.getObjPath(element.bgcflj);
      }
      if (element.spcflj) {
        element.spcflj = this.getObjPath(element.spcflj);
      }
    });
    return data;
  }

  /**
   * 所有服务同意调用口
   * @param serviceID  服务/接口ID
   * @param param 参数
   * @param forArr 字段转换数组
   * @param collback 回调函数 -- 返回处理好的数据
   */

  backServiceData(serviceID, params, forArr, collback) {
      this.getList(serviceID, params).subscribe(res => {
      if (res.code === 1 && res.data) {
        const list = this.mainFormatService.fieldAdaptive(res.data.list, forArr);
        const rlist = this.dataFormat(list);
        const totalRecord = res.data.totalRecord;
        collback(rlist, totalRecord);
      } else {
        collback(null, 0);
      }
    });
  }
}
